package com.example.helloworld;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class DisplayAvatarActivity extends Activity{
    
    private static ArrayList<Integer> headList;
    private static ArrayList<Integer> bodyList;
    private static ArrayList<Integer> legList;
    private static int headPosition;
    private static int bodyPosition;
    private static int legPosition;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_avatar);
        // Populate the head, body, and legs lists
        populateAvatarLists();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_avatar, menu);
        return true;
    }

    public void nextHead(View view) throws InterruptedException{
        if(headPosition < headList.size() - 1){
            headPosition++;
            ImageView bodyImage = (ImageView) findViewById(R.id.headImageView);
            bodyImage.setImageResource(headList.get(headPosition));
        }
    }
    
    public void previousHead(View view) throws InterruptedException{
        if(headPosition > 0){
            headPosition--;
            ImageView bodyImage = (ImageView) findViewById(R.id.headImageView);
            bodyImage.setImageResource(headList.get(headPosition));
        }
    }
    
    public void nextBody(View view) throws InterruptedException{
        // If we have another image in the list ahead of this one, switch to it
        if(bodyPosition < bodyList.size() - 1){
            bodyPosition++;
            ImageView bodyImage = (ImageView) findViewById(R.id.bodyImageView);
            bodyImage.setImageResource(bodyList.get(bodyPosition));
        }
    }
    
    public void previousBody(View view) throws InterruptedException{
        if(bodyPosition > 0){
            bodyPosition--;
            ImageView bodyImage = (ImageView) findViewById(R.id.bodyImageView);
            bodyImage.setImageResource(bodyList.get(bodyPosition));
        }
    }
    public void nextLegs(View view) throws InterruptedException{
        if(legPosition < legList.size() - 1){
            legPosition++;
            ImageView bodyImage = (ImageView) findViewById(R.id.legsImageView);
            bodyImage.setImageResource(legList.get(legPosition));
        }
    }
    
    public void previousLegs(View view) throws InterruptedException{
        if(legPosition > 0){
            legPosition--;
            ImageView bodyImage = (ImageView) findViewById(R.id.legsImageView);
            bodyImage.setImageResource(legList.get(legPosition));
        }
    }

    private void populateAvatarLists() {
        // Populate the avatar lists with the appropriate resources
        populateHeadList();
        populateBodyList();
        populateLegsList();
    }

    private void populateHeadList() {
        // Populate an ArrayList with the head options
        headList = new ArrayList<Integer>();
        /*
         * Resources have to be listed statically from the R class if they are placed
         * in the drawable**** folders.
         * The R class is a generated class that cannot and should not be changed manually.
         */
        headList.add(R.drawable.bhead);
        headList.add(R.drawable.ghead);
        headList.add(R.drawable.rhead);
        headList.add(R.drawable.yhead);
        headPosition = 0;
        ImageView headImage = (ImageView) findViewById(R.id.headImageView);
        headImage.setImageResource(headList.get(headPosition));
    }

    private void populateBodyList() {
        // Populate an ArrayList with the body options
        // Also set the default image at the first body option
        bodyList = new ArrayList<Integer>();
        bodyList.add(R.drawable.bbody);
        bodyList.add(R.drawable.gbody);
        bodyList.add(R.drawable.rbody);
        bodyList.add(R.drawable.ybody);
        bodyPosition = 0;
        ImageView bodyImage = (ImageView) findViewById(R.id.bodyImageView);
        bodyImage.setImageResource(bodyList.get(bodyPosition));
    }

    private void populateLegsList() {
        // Populate an ArrayList with the leg options
        legList = new ArrayList<Integer>();
        legList.add(R.drawable.blegs);
        legList.add(R.drawable.glegs);
        legList.add(R.drawable.rlegs);
        legList.add(R.drawable.ylegs);
        legPosition = 0;
        ImageView legsImage = (ImageView) findViewById(R.id.legsImageView);
        legsImage.setImageResource(legList.get(legPosition));
    }

}
